'use strict';

angular.module('app')
    .controller('updateController', ['$scope', '$rootScope', '$state', '$timeout',
        function($scope, $rootScope, $state, $timeout) {
            var title = "应付款新增";
            $scope.title = $rootScope.title = title;
            $scope.loading = true;
            $scope.record={};
            selSupplyList();
            $scope.record.receAcctType ="3";
            //初始化验证
            $scope.osubmit = function(status){
                $scope.loading = true
                $scope.record.planApplyDate=$('#planApplyDate').val();
                $scope.record.status=status;
                if($scope.record.extractAmount<=0){
                 $scope.alertModal({'content':'提现金额必须大于0'});
                 return;
                }
                 $('#form').submit();
            }

            // 查询正常的流量主
            function selSupplyList() {
                $scope.loading = true;
                $.ajax({
                    url: $scope.app.project_name + '/extractApply/read/supplyList',
                    data: {}
                }).then(function (result) {
                    $scope.loading = false;
                    if (result.httpCode == 200) {
                        $scope.supplyList = result.data;
                        $scope.record.applyUserId = $scope.supplyList[0].userIdS;
                    } else {
                        $scope.msg = result.msg;
                    }
                    $scope.$apply();
                });
            }
            //查询银行
            function selBank(){
                $scope.loading = true;
                $.ajax({
                    url: $scope.app.project_name + '/extractApply/read/bankList',
                    data: {}
                }).then(function (result) {
                    $scope.loading = false;
                    if (result.httpCode == 200) {
                        $scope.bankList = result.data;
                        $scope.record.bankCardName = $scope.bankList[0].userIdS;
                    } else {
                        $scope.msg = result.msg;
                    }
                    $scope.$apply();
                });
                
            }
           $('#form').validate({
                rules: {
                    extractAmount: {
                        required: true
                    },
                    bankCardNo: {
                        required: true
                    },
                    bankName: {
                        required: true
                    },
                    bankCardName: {
                        required: true
                    },
                    planApplyDate: {
                        required: true
                    },
                    supplyInfo: {
                        required: true
                    }


                },
                messages: {
                    extractAmount: {
                        required: '请输入付款金额'
                    },
                    bankCardNo: {
                        required: '请输入收款账户账户'
                    },
                    bankName: {
                        required: '请输入行别'
                    },
                    bankCardName: {
                        required: '请输入开户行'
                    },
                    planApplyDate: {
                        required: '请输入计划打款日期'
                    },
                    supplyInfo: {
                        required: '请输入汇款单位'
                    }



                },
               submitHandler: function() {
                   var m = $scope.record;
                   $scope.isDisabled = true;//提交disabled
                   $.ajax({
                       url:$scope.app.project_name + '/extractApply/add/addExtract',
                       data: m
                   }).then(function (result) {
                       $scope.loading = false;
                       if (result && result.httpCode == 200) {//成功
                           if(result.data>0){
                               $scope.alertModal({"content":"提现申请成功"});
                               if($scope.record.status==1){
                                   $scope.record={};
                                   $scope.record.receAcctType ="3";
                                   $scope.record.applyUserId = $scope.supplyList[0].userIdS;
                                   $state.go('main.finance.extractApply.create');
                                   $scope.isDisabled = false;//提交disabled
                                   $('#planApplyDate').val("");
                               }else{
                                   $state.go('main.finance.extractApply.list');
                               }
                           }else if(result.data==-1) {
                               $scope.isDisabled = false;//提交disabled
                               $scope.alertModal({"content":"提现申请金额不能大于账户余额"});

                           }else{
                               $scope.isDisabled = false;//提交disabled
                               $scope.alertModal({"content":"该账户没有主账户,请确认后再次申请"});
                           }
                       } else {
                           $scope.isDisabled = false;//提交disabled
                           $scope.alertModal({"content":"提现申请失败"});
                       }
                       $scope.$apply();
                   });
               }
           });










        }]);